package com.wx.util;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

@Slf4j
public class IpUtil {

    public static boolean isPortBeUse(int port){
        try {
            Socket localhost = new Socket("localhost", port);
            localhost.close();
            return true;
        } catch (IOException e) {
            log.info("{}端口号已经被占用",port);
        }
        return false;
    }
    public static int getUserfulPort(int port){

        while (isPortBeUse(port)){
            port++;
        }
        return port;
    }
    public static String getHost(){
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getLocalIp(){
        InetAddress inetAddress= null;
        try {
            inetAddress = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        String ip=inetAddress.getHostAddress();//获得本机Ip
        return ip;
    }
}
